﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Diagnostics;

namespace BAASearchLib
{
    public class DocToText
    {
        private object filePath;
        private object objFalse = false;
        private object objTrue = true;
        private object missing = System.Reflection.Missing.Value;
        private object emptyData = string.Empty;
        private string author = "";

        public DocToText(string filePath)
        {
            this.filePath = filePath;
        }

        /// <summary>
        /// Opens actually the file in the background 
        /// -> so it is not so fast, may be fixed in the second release (which we will not have) *sad and cry painfully*
        /// </summary>
        /// <returns></returns>
        public string ExtractText()
        {
            Microsoft.Office.Interop.Word.Document aDoc = null;

            try
            {
                DocToTextWordApp.WordApp.Visible = false;
                aDoc = DocToTextWordApp.WordApp.Documents.Open(ref filePath, ref missing, ref objTrue,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref objFalse, ref missing,
                    ref missing, ref missing, ref missing);


                aDoc.ActiveWindow.Selection.WholeStory();
                aDoc.ActiveWindow.Selection.Copy();

                IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
                String filetext = data.GetData(System.Windows.Forms.DataFormats.Text).ToString();
                System.Windows.Forms.Clipboard.SetDataObject(string.Empty);

                return filetext;
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("PATH: " + filePath +
                    "\nThe Data object is null, could not get the ClipBoard dataobject" +
                    "\nAND I DON'T KNOW WHY. Why is it working in the test but here...\n" + ex.Message);
                return "";
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
            finally
            {
                (aDoc as _Document).Close(ref objFalse, ref missing, ref missing);
            }
        }

        public string GetAuthor()
        {
            Microsoft.Office.Interop.Word.Document aDoc = null;
            try
            {
                aDoc = DocToTextWordApp.WordApp.Documents.Open(ref filePath, ref missing, ref objTrue,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref objFalse, ref missing,
                    ref missing, ref missing, ref missing);
                // -----------Gets the Author-----------
                object oDocBuiltInProps = aDoc.BuiltInDocumentProperties;
                Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

                // Get the Author property and display it.
                string strIndex = "Author";
                string strValue;
                object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
                                           BindingFlags.Default |
                                           BindingFlags.GetProperty,
                                           null, oDocBuiltInProps,
                                           new object[] { strIndex });
                Type typeDocAuthorProp = oDocAuthorProp.GetType();
                strValue = typeDocAuthorProp.InvokeMember("Value",
                                           BindingFlags.Default |
                                           BindingFlags.GetProperty,
                                           null, oDocAuthorProp,
                                           new object[] { }).ToString();
                this.author = strValue;
                //MessageBox.Show("The Author is: " + strValue, "Author");
                // --------------------------------------------------
            }
            catch (Exception e)
            {

                throw new ArgumentException(e.Message);
            }
            finally
            {
                (aDoc as _Document).Close(ref objFalse, ref missing, ref missing);
            }

            return author;
        }
    }
}
